-
Notifications
You must be signed in to change notification settings - Fork 1
/
question 13.cpp
20 lines (20 loc) · 953 Bytes
/
question 13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Question 13
// The square root of a number N can be approximated by a repeated calculation using the following formula.
// NewGuess = 0.5(LastGuess + N /LastGuess)
// where NewGuess is the next guess and LastGuess the last guess. The calculation of a NewGuess should be terminated when the absolute value of the difference between the NewGuess and the LastGuess is about 0.0000001. Write down a computer solution for the above formula. You may use the function ABS(x) or '|x|' to obtain the absolute value of x.
#include <iostream>
using namespace std;
int main(){
float n, lastGuess, newGuess;
cout << "Enter a number: ";
cin >> n;
lastGuess = 1;
newGuess = 0.5 * (lastGuess + n / lastGuess);
while (abs(newGuess - lastGuess) >= 0.0000001)
{
lastGuess = newGuess;
newGuess = 0.5 * (lastGuess + n / lastGuess);
}
cout << "The square root of " << n << " is " << newGuess << endl;
return 0;
}